import role from '../../config/role';
import config from "../../config/config";
Page({
  data: {
    pageTopHeight: wx.getWindowInfo().statusBarHeight,
    role:JSON.parse(JSON.stringify(role)),
    routerRole:new Map(),
    count:0,
    modalCole:0,
    speedyCreate: config.speedyCreate,
    ownGameMessage:{},
    picker: false,
    numbers: [4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],
    indexValue: [2], // 默认选中6（索引为3）
    selectedValue: 6,
    roleIndex: 0
  },

  onLoad() {
    this.setData({ 
      routerRole:new Map(),
      count:0,
      selectedValue: this.data.numbers[this.data.indexValue[0]]
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },
  /**
   * 生命周期函数--监听页面隐藏 客户端离开房间
   */
  onHide() {
  
  },
  // 销毁
  onUnload() {
   
  },
  
  // 关闭弹窗
  close(){
    this.setData({modalCole:0})
  },

  // 参与
  attend(){
    const roomArray = Array.from(this.data.routerRole)
    const room = JSON.stringify(roomArray)
    // console.log(room,'====room=====') // [[1,3],[5,1],[7,1]]
    wx.navigateTo({
      url: `/pages/sRoom/sRoom?room=${room}&partIn=1`
    })
    this.setData({modalCole:0})
  },

  // 不参与
  attendNot(){
    this.setData({modalCole:0})
    const roomArray = Array.from(this.data.routerRole)
    const room = JSON.stringify(roomArray)
    wx.navigateTo({
      url: `/pages/sRoom/sRoom?room=${room}&partIn=0`
    })
  },
  // 返回
  goPage(){
    const pages = getCurrentPages();
    const routeStackLength = pages.length;
    routeStackLength > 1 ? wx.navigateBack({ delta: 2}) : wx.redirectTo({ url: '/pages/home/home' })
  },
  // 点击描述 查看游戏规则
  seeDescribe(e){
    const roleIndex = e.target.dataset.index
    this.setData({modalCole:15,roleIndex})

  },
  // 快速选择
  pickerShow(){
    if (this.data.count > 3 && this.data.count < 20) {
      const indexVal = this.data.numbers.findIndex(d => d === this.data.count)
      this.setData({ indexValue: [indexVal], selectedValue:this.data.count })
    }else{
      this.setData({ indexValue: [2], selectedValue:6 })
    }
    this.setData({picker:true})
  },
  // 关闭快速选择
  pickerClose(e){
    const close = e.target.dataset.close
    if(close){
      this.setData({picker:false})
    }
  },
  // 确认快速选择
  pickerOk(){
    const mapData = config.speedyCreate[this.data.selectedValue];
    const newRole = []
    const roleArr = JSON.parse(JSON.stringify(role))
    roleArr.forEach((element) => {
      mapData.forEach( m => {
        if(element.code === m[0]){
          element.number = m[1]
        }
      })
      newRole.push(element)
    })
    this.setData({
      role:newRole,
      count: this.data.selectedValue
    });
    this.setData({picker:false})
  },
  // 选择器变化事件
  bindChange: function(e) {
    const val = e.detail.value[0];
    this.setData({
      indexValue: [val],
      selectedValue: this.data.numbers[val]
    });
  },
  //  添加角色
  addRole(e){
    let count = this.data.count
    count++
    const item = e.currentTarget.dataset.item
    item.number++
    const role = this.data.role
    const index = role.findIndex(d=>d.code === item.code)
    role.splice(index, 1, item)
    this.setData({ role, count })
  },
  //  减少角色
  reduceRole(e){
    let count = this.data.count
    count--
    const item = e.currentTarget.dataset.item
    item.number--
    const role = this.data.role
    const index = role.findIndex(d=>d.code === item.code)
    role.splice(index, 1, item)
    this.setData({ role, count })
  },
  // 创建房间
  createRoom(){
    if(this.data.count === 0){
      wx.showToast({
        title: '请选择角色',
        icon: 'none',
        duration: 2000
      })
      return
    }
    let goodCount = 0
    let badCount = 0

    const role = JSON.parse(JSON.stringify(this.data.role))
    const routerRole = new Map()
    role.forEach((element) => {
      if(element.number > 0){
        routerRole.set(element.code,element.number)
      }
      if(element.good){
        goodCount = goodCount + element.number
      }else{
        badCount = badCount + element.number
      }
    });
    // if(goodCount <= badCount){
    //   wx.showToast({
    //     title: '好人数量大于狼人数量',
    //     icon: 'none',
    //     duration: 2000
    //   })
    //   return
    // }
    this.setData({modalCole:2,routerRole})
  },
  // 重置
  reset(){
    this.setData({
      role:JSON.parse(JSON.stringify(role)),
      routerRole:[],
      count:0,
    })
  },
  
});
